package com.adv.ems.model.vo;

import cn.hutool.json.JSONUtil;
import com.adv.ems.db.entity.BuildingInfoDO;
import com.adv.ems.db.entity.DepartmentInfoDO;
import com.adv.ems.db.entity.InstituteInfoDO;
import com.adv.ems.db.entity.SystemMenuDO;
import com.adv.ems.model.dto.SatelliteLocationDTO;
import com.adv.ems.utils.EmsUtil;
import lombok.Data;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

@Data
public class LoginVO {
    private String username;
    private String nick;
    private String token;
    private List<SubjectInfo> institutes;
    private List<SubjectInfo> buildings;
    private List<SubjectInfo> departments;
    private List<Menu> menus;

    public LoginVO() {
        this.institutes = new ArrayList<SubjectInfo>();
        this.buildings = new ArrayList<SubjectInfo>();
        this.departments = new ArrayList<SubjectInfo>();
        this.menus = new ArrayList<>();
    }

    @Data
    public static class Menu{
        private Long id;
        private Long parentId;
        private String name;
        private String icon;
        private String path;
        private List<Menu> children;
        public Menu(){
            this.children = new ArrayList<Menu>();
        }

        public Menu(SystemMenuDO menuDO){
            this.id = menuDO.getId();
            this.parentId = menuDO.getParentId();
            this.name = menuDO.getName();
            this.icon = menuDO.getIcon();
            this.path = menuDO.getPath();
            this.children = new ArrayList<Menu>();
        }
    }

    @Data
    public static class SubjectInfo {
        private String name;
        private String logo;
        private Integer meterNum;
        private Integer buildingNum;
        private String areaSize;
        private String profile;
        private String address;
        private SatelliteLocationDTO location;

        public static SubjectInfo build(InstituteInfoDO institute, Integer buildingNum, Integer meterNum, String acceptLanguage) {
            SubjectInfo subjectInfo = new SubjectInfo();
            subjectInfo.setName(EmsUtil.getNameInfo(institute.getNameInfo(), acceptLanguage).getShortName());
            subjectInfo.setLogo(institute.getLogo());
            subjectInfo.setMeterNum(meterNum);
            subjectInfo.setBuildingNum(buildingNum);
            subjectInfo.setAreaSize(institute.getBuildingSize() + "㎡");
            subjectInfo.setProfile(institute.getProfile());
            subjectInfo.setAddress(institute.getAddress());
            if (StringUtils.hasLength(institute.getSatelliteLocation())) {
                subjectInfo.setLocation(EmsUtil.formatLocation(institute.getSatelliteLocation()));
            }
            return subjectInfo;
        }

        public static SubjectInfo build(BuildingInfoDO building, Integer meterNum, String acceptLanguage) {
            SubjectInfo subjectInfo = new SubjectInfo();
            subjectInfo.setName(EmsUtil.getNameInfo(building.getNameInfo(), acceptLanguage).getShortName());
            subjectInfo.setLogo(building.getLogo());
            subjectInfo.setMeterNum(meterNum);
            subjectInfo.setAreaSize(building.getBuildingSize() + "㎡");
            subjectInfo.setProfile(building.getProfile());
            subjectInfo.setAddress(building.getAddress());
            if (StringUtils.hasLength(building.getSatelliteLocation())) {
                subjectInfo.setLocation(EmsUtil.formatLocation(building.getSatelliteLocation()));
            }
            return subjectInfo;
        }

        public static SubjectInfo build(DepartmentInfoDO department, Integer meterNum, String acceptLanguage) {
            SubjectInfo subjectInfo = new SubjectInfo();
            subjectInfo.setName(EmsUtil.getNameInfo(department.getNameInfo(), acceptLanguage).getShortName());
            subjectInfo.setLogo(department.getLogo());
            subjectInfo.setMeterNum(meterNum);
            subjectInfo.setProfile(department.getProfile());
            subjectInfo.setAreaSize(department.getTotalSize() + "㎡");
            subjectInfo.setAddress(department.getAddress());
            if (StringUtils.hasLength(department.getSatelliteLocation())) {
                subjectInfo.setLocation(EmsUtil.formatLocation(department.getSatelliteLocation()));
            }
            return subjectInfo;
        }
    }
}
